package com.hsc.eduservice.controller;


import com.hsc.commonutils.R;
import com.hsc.eduservice.entity.EduCourse;
import com.hsc.eduservice.entity.vo.CourseInfoVo;
import com.hsc.eduservice.entity.vo.CoursePublishVo;
import com.hsc.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySources;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin //gateway解决了可以注掉
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    // TODO 有待完善，条件查询和分页
    //查询所有课程列表
    @GetMapping()
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    @PostMapping("addCourseInfo")
    public R addCouserInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回课程id，为了后面添加大纲使用
        String id = courseService.saveCouserInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    /**
     * 根据课程id查询课程信息
     * @param courseId 课程id
     * @return 课程信息
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }


    /**
     * 根据课程id修改课程信息
     * @param courseInfoVo 课程信息
     * @return
     */
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

